<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <template #after>
          <el-button type="primary" size="small" @click="addPermission(1,'0')"><i class="el-icon-plus" />添加权限</el-button>
        </template>
      </PageTools>
      <!-- 页面权限数据渲染 -->
      <el-table :data="permissionList" row-key="id">
        <el-table-column label="权限名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button :disabled="row.type === 2" type="text" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加权限和编辑权限 -->
      <el-dialog :title="`${showText}权限点`" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  delPermission,
  getPermissionDetail
  // updatePermission
} from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      permissionList: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        // 控制默认开关按钮
        enVisible: '1'
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 10, message: '长度不能超过十个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 30, message: '长度不能超过三十个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionList()
      this.permissionList = listToTreeData(res, '0')
      // console.log(res)
    },
    // 新增权限
    addPermission(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 点击编辑
    async editPermission(id) {
      this.showDialog = true
      this.formData = await getPermissionDetail(id)
    },
    // 确定新增权限
    async btnOK() {
      try {
        // 验证
        await this.$refs.perForm.validate()
        // 发请求
        if (this.formData.id) {
          // 编辑
        } else {
          // 新增
          await addPermission(this.formData)
        }
        // 提示用户
        this.$message.success('操作成功')
        // 更新页面
        this.getPermissionList()
        // 关掉弹窗
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 点击取消
    btnCancel() {
      // 清空表单
      this.formData = {
        name: '',
        code: '',
        description: '',
        // 控制默认开关按钮
        enVisible: '1'
      }
      // 清空验证
      this.$refs.perForm.resetFields()
      // 关闭弹窗
      this.showDialog = false
    },
    // 删除权限
    async delPermission(id) {
      // 二次确认
      await this.$confirm('确定删除吗')
      // 发请求
      await delPermission(id)
      // 提示用户
      this.$message.success('删除成功')
      // 更新页面
      this.getPermissionList()
    }
  }
}
</script>

<style>

</style>
